/**
 * Copyright 2011 Poltman.Ltd; www.poltman.com. All rights reserved.		
 *
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */

package org.dspace.handle;

import org.apache.log4j.Logger;
import org.zkoss.zkplus.spring.SpringUtil;

import com.poltman.dscentral.core.ConfigurationManager;
import com.poltman.dscentral.db.entity.dspace.HandleEntity;
import com.poltman.dscentral.db.exceptions.RepositoryAccessException;
import com.poltman.dscentral.db.exceptions.RepositoryException;
import com.poltman.dscentral.db.service.dspace.HandleService;


/**
 * 
 * @author z.ciok@poltman.com
 *
 */
public class HandleManager {
    /** log4j category */
    private static Logger log = Logger.getLogger(HandleManager.class);

    /** Prefix registered to no one */
    static final String EXAMPLE_PREFIX = "123456789";

    /** Private Constructor */
    private HandleManager() { }


    /**
     * 
     * @param resourceTypeId
     * @param resourceId
     * @return
     * @throws Exception
     */
    public static String createHandle(Integer resourceTypeId, Integer resourceId) throws RepositoryAccessException, RepositoryException {
    	
    	String handleId = null;
    	try {
    		HandleEntity handle = new HandleEntity();
    		handle.setResourceTypeId(resourceTypeId);
    		handle.setResourceId(resourceId);
    		HandleService handleService = (HandleService) SpringUtil.getBean("handleService");
    		handle = handleService.save(handle);
        
    		handleId = createId(handle.getHandleId());
    		handleService.setFixedHandleFor(handleId, handle.getHandleId());
    	} catch (RepositoryException e) {
    		log.error("HandleManager.createHandle Error: ", e);
    		throw new RepositoryException("HandleManager.createHandle Error: ", e);
    	} catch (RepositoryAccessException rae) {
    		log.error("HandleManager.createHandle Error: ", rae);
    		throw new RepositoryAccessException("HandleManager.createHandle Error: ", rae);
    	}
		
        return handleId;
    }
    
    /**
     * Get the configured Handle prefix string, or a default
     * @return configured prefix or "123456789"
     */
    public static String getPrefix() {
        String prefix = ConfigurationManager.getProperty("handle.prefix");
        if (null == prefix) {
            prefix = EXAMPLE_PREFIX; // XXX no good way to exit cleanly
            log.error("handle.prefix is not configured; using " + prefix);
        }
        return prefix;
    }

    /**
     * Create a new handle id. The implementation uses the PK of the RDBMS
     * Handle table.
     *
     * @return A new handle id
     *
     */
    private static String createId(int id) {
        String handlePrefix = getPrefix();

        return new StringBuffer().append(handlePrefix).append(
                handlePrefix.endsWith("/") ? "" : "/").append(id).toString();
    }
}
